import {cancelPmForm, exportPm, selectPmFormByPage} from '@/api/assessment'
import {CCard} from '@/components/card'
import {CnMediumP, CnMediumSpan, CnNormalP, CnNormalSpan} from '@/components/text'
import {useTablePaging, useTableLoading} from '@/hooks/tableHook'
import {toInitiateAssessment, toInitiateAssessmentCheck} from '@/libs/pageRedirect'
import {downFile} from '@/libs/toolkit'
import {throttle} from '@/libs/debounce'
import {getUserId} from '@/libs/auth'
import {getStorage} from '@/libs/localstoreage'
import {getSession} from '@/libs/sessionStorage'
import { defineComponent, reactive, onMounted } from 'vue'

export default defineComponent({
  name: 'assessmentInitiated',
  setup(props, ctx) {
    const {page, changePage, size, changeTableTotal, getPagination} = useTablePaging({
      page: 1,
      size: 10
    })

    const {startLoading, closeLoading, loading} = useTableLoading()

    let state = reactive<{
      dataSource: PmForm.TableData[]
      pmFormName: string
    }>({
      dataSource: [],
      pmFormName: ''
    })
    const tableColumns: CColumn<PmForm.TableData>[] = [
      {
        title: '考核名称',
        dataIndex: 'pmFormName'
      },
      {
        title: '时间周期',
        dataIndex: 'dateYear'
      },
      {
        title: '参与人数',
        dataIndex: 'involvementUserCount'
      },
      {
        title: '状态',
        dataIndex: 'status'
      },
      {
        title: '操作',
        dataIndex: 'utils',
        width: 200
      }
    ]

    function getTableData() {
      startLoading()
      selectPmFormByPage({
        page: page.value,
        size: size.value,
        pmFormName: state.pmFormName
      })
        .then(res => {
          state.dataSource = res.data.list
          changeTableTotal(res.data.total)
          closeLoading()
        })
        .catch(err => {
          closeLoading()
        })
    }

    const searchInput = throttle(() => {
      if (page.value !== 1) {
        changePage(1)
      }
      getTableData()
    }, 300)

    function getPageData() {
      getTableData()
    }

    onMounted(() => {
      getPageData()
    })

    return () => (
      <div class='CBC-bg-toRight p-16 rounded-4'>
        <div class='mb-16 flex justify-between items-center gap-x-16'>
          <div>
            <a-input
              class='w-222 rounded-4'
              placeholder='输入考核名称搜索'
              v-slots={{
                prefix: () => <i class='iconfont icon-fangdajing text-infoHalf text-13'></i>
              }}
              v-model={[state.pmFormName, 'value']}
              onInput={searchInput}
            ></a-input>
          </div>
          <div class='flex items-center gap-x-12'>
            <a-button
              class='rounded-4 bg-primary text-white'
              type='primary'
              onClick={() => {
                toInitiateAssessment()
              }}
            >
              发起考核
            </a-button>
          </div>
        </div>
        <CCard title='已发起的考评组' showTitle={false}>
          <a-table
            class='custom-table'
            loading={loading.value}
            pagination={getPagination({
              getTableData
            })}
            dataSource={state.dataSource}
            columns={tableColumns}
            v-slots={{
              bodyCell: ({text, record, index, column}: CBodyCell<PmForm.TableData>) => {
                switch (column.dataIndex) {
                  case 'status':
                    return record.status ? (
                      <CnNormalP
                        class='p-16 text-12 cursor-pointer '
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      >
                        正常
                      </CnNormalP>
                    ) : (
                      <CnNormalP
                        class='p-16 text-danger text-12 cursor-pointer '
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      >
                        已撤销
                      </CnNormalP>
                    )
                  case 'dateYear':
                    let value = ''
                    switch (record.datePeriodTypeCode) {
                      case '0025':
                        value = record.dateCount + '月'
                        break
                      case '0026':
                        value = '第' + record.dateCount + '季度'
                        break
                      case '0028':
                        value = ''
                        break
                      case '0027':
                        value = record.dateCount === 1 ? '上半年' : '下半年'
                        break
                      // case '0029':
                      //   return (
                      //     <CnNormalP
                      //       class='p-16 cursor-pointer text-12'
                      //       onClick={() => {
                      //         toInitiateAssessmentCheck({pmFormId: record.pmFormId})
                      //       }}
                      //     >
                      //       自定义
                      //     </CnNormalP>
                      //   )
                      default:
                        value = record.dateCount ? `${record.dateCount}月` : ''
                        break
                    }
                    return (
                      <CnNormalP
                        class='p-16 cursor-pointer text-12'
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      >
                        {record.dateYear}年{value}
                      </CnNormalP>
                    )
                  case 'involvementUserCount':
                    return (
                      <CnNormalP
                        class='p-16 cursor-pointer text-12'
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      >
                        {record.involvementUserCount}人
                      </CnNormalP>
                    )
                  case 'utils':
                    return record.status ? (
                      <div class='p-16 flex items-center gap-x-8'>
                        <CnNormalSpan
                          class='text-primary cursor-pointer text-12'
                          onClick={() => {
                            toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                          }}
                        >
                          查看
                        </CnNormalSpan>
                        <CnNormalSpan
                          class='text-primary cursor-pointer text-12'
                          onClick={() => {
                            exportPm({pmFormId: record.pmFormId}).then(res => {
                              console.log(record.pmFormName)

                              downFile(res as unknown as Blob, record.pmFormName)
                            })
                          }}
                        >
                          导出
                        </CnNormalSpan>
                        <a-popconfirm
                          title={`确认撤销已发起的《${record.pmFormName}》`}
                          ok-text='确认'
                          cancel-text='取消'
                          onConfirm={() => {
                            let makeUserDDId = getSession('userDDId')
                            if (!makeUserDDId) return
                            cancelPmForm({
                              pmFormId: record.pmFormId,
                              makeUserDDId: makeUserDDId
                            })
                              .then(res => {
                                getTableData()
                              })
                              .catch(err => {})
                          }}
                        >
                          <CnNormalSpan class='text-danger cursor-pointer text-12'>
                            撤销
                          </CnNormalSpan>
                        </a-popconfirm>
                      </div>
                    ) : (
                      <CnNormalSpan
                        class='p-16 text-danger cursor-pointer  text-12'
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      ></CnNormalSpan>
                    )
                  default:
                    return (
                      <CnNormalP
                        class='p-16 cursor-pointer text-12'
                        onClick={() => {
                          toInitiateAssessmentCheck({pmFormId: record.pmFormId, isRefersh: 1})
                        }}
                      >
                        {text}
                      </CnNormalP>
                    )
                }
              }
            }}
            bordered
          ></a-table>
        </CCard>
      </div>
    )
  }
})
